home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug236 / xc.c < prev    next >
Text File  |  1980-01-02  |  26KB  |  884 lines

  1. /*
  2.     HEADER:        CUG000.00;
  3.     TITLE:        Cross Reference Generator;
  4.     DATE:        04/05/1987;
  5.     DESCRIPTION:    "Generic version of programmer's cross-reference
  6.             generator originally for BDS.";
  7.     VERSION:    2.2;
  8.     KEYWORDS:    Cross Reference;
  9.     FILENAME:    XC.C;
  10.     SEE-ALSO:    CUG126, CUG171;
  11.     COMPILERS:    vanilla;
  12.     AUTHORS:    Phillip N. Hisley, W. H. Rauser, W. C. Colley III;
  13. */
  14.  
  15. /************************************************************************/
  16. /*   5 APR 1987      Portable C Conversion        WCC                */
  17. /*     -  Epson init string changed from hex (\x00) to octal (\000).    */
  18. /*     -  Use () with A?(c=B):(c=C) for portability.  If it's an error, */
  19. /*        it seems to be a very common one.            */
  20. /*     -  Made use of host compiler's strcmp() and strcpy() as they are */
  21. /*        usually written in assembly and run faster.        */
  22. /*     -  Globally replaced "alpha_" with "alp_", "alloc_" with        */
  23. /*        "allo_", and "tmpchr" with "tmpch" to avoid multiply    */
  24. /*        defined symbol errors on compilers that only support    */
  25. /*        @$%&*&?>}\~~@# 6-character names.            */
  26. /*     -  Added abs() macro for compilers that don't have one.          */
  27. /*     -  Used some data initialization to kill off some code.        */
  28. /*     -  Made miscellaneous small tweaks for portability.        */
  29. /*     -  Cleaned out old BDS C stuff to improve readability.        */
  30. /*     -  Cleaned up abortion with use of rf_blk.rf_cnt as a pointer    */
  31. /*        link to the next rf_blk.  This construct will NOT work    */
  32. /*        in environments like the MSDOS large model where ints    */
  33. /*        are smaller than pointers!                */
  34. /*                                    */
  35. /*   9-26-83   Microsoft C 1.04     Conversion    WHR            */
  36. /*     -  \t between line numbers and text to fix indenting problem.    */
  37. /*     -  added option -e for output to Epson in condensed print.    */
  38. /*     -  toupper() and isupper() are macros, not functions.        */
  39. /*     -  eliminate side effect in toupper(*++arg) in main().        */
  40. /*     -  change alloc() to malloc().                    */
  41. /*     -  add #define NAMES that are not in stdio.h            */
  42. /*     -  MS-C requires () in statement A?(c=B):(c=C)    error or not??    */
  43. /*                                    */
  44. /*   4-30-83   Computer Innovations C-86 1.31 Conversion    WHR        */
  45. /*     -  #include filename changed to allow a disk drive prefix, D:    */
  46. /*     -  convert if(fprintf(...) == ERROR) lst_err(); to fprintf(..);    */
  47. /*     -  convert if(fopen(...) == ERROR) statements to == NULL.    */
  48. /*     -  C86 requires () in statement A?(c=B):(c=C)    error or not??    */
  49. /*     -  remove getc() == ERROR check in fil_chr().            */
  50. /*     -  convert file conventions from BDS C to C-86.            */
  51. /*     -  comment out BDS unique statements, mark revised statements.    */
  52. /*      keep all BDS statements to document conversion effort.    */
  53. /*                                    */
  54. /**  4-19-83   BDS C Version file XC.CQ copied from Laurel RCPM       WHR    */
  55. /************************************************************************/
  56. /*                               */
  57. /*    XC  -  A 'C' Concordance Utility               */
  58. /*                               */
  59. /*    Version 1.0   January, 1982               */
  60. /*                               */
  61. /*    Copyright (c) 1982 by Philip N. Hisley           */
  62. /*                               */
  63. /*    Released for non-commercial distribution only       */
  64. /*                               */
  65. /*    Abstract:                           */
  66. /*                               */
  67. /*    'XC' is a cross-reference utility for 'C' programs.  */
  68. /*    Its has the ability to handle nested include files   */
  69. /*    to a depth of 8 levels and properly processes nested */
  70. /*    comments as supported by BDS C. Option flags support */
  71. /*    the following features:                   */
  72. /*                               */
  73. /*    - Routing of list output to disk               */
  74. /*    - Cross-referencing of reserved words           */
  75. /*    - Processing of nested include files           */
  76. /*    - Generation of listing only               */
  77. /*                               */
  78. /*    Usage: xc <filename> <flag(s)>               */
  79. /*                               */
  80. /*    Flags: -i           = Enable file inclusion       */
  81. /*         -l           = Generate listing only       */
  82. /*         -r           = Cross-ref reserved words       */
  83. /*         -o <filename> = Write output to named file       */
  84. /*                               */
  85. /*    Please report bugs/fixes/enhancements to:           */
  86. /*                               */
  87. /*          Philip N. Hisley                   */
  88. /*          548H Jamestown Court               */
  89. /*          Edgewood, Maryland 21040               */
  90. /*          (301) 679-4606                   */
  91. /*          Net Addr: PNH@MIT-AI               */
  92. /*                               */
  93. /***********************************************************/
  94.  
  95. #include <stdio.h>
  96.  
  97. /*
  98.  * Portability Note:  The AZTEC C compilers handle the binary/text file
  99.  * dichotomy differently from most other compilers.  Uncomment the following
  100.  * pair of #defines if you are running AZTEC C:
  101.  */
  102.  
  103. /*
  104. #define getc(f)        agetc(f)
  105. #define putc(c,f)    aputc(c,f)
  106. */
  107.  
  108. /*  Portability Note:  8-bit systems often don't have header files
  109.     ctype.h and string.h.  If your system doesn't have these,
  110.     uncomment the #defines NO_STRING_H and NO_CTYPE_H.           WCC    */
  111.  
  112. /*
  113. #define     NO_STRING_H
  114. #define     NO_CTYPE_H
  115. */
  116.  
  117. /*  Portability Note:  Some older compilers call the function malloc()
  118.     by its older name alloc().    If you have one of these older
  119.     compilers, uncomment the following #define.            WCC    */
  120.  
  121. /*
  122. #define     malloc(x)    alloc(x)
  123. */
  124.  
  125. /*  Portability Note:  A few compilers don't know the additional type
  126.     void.  If yours is one of these, uncomment the following #define.    */
  127.  
  128. /* #define    void        int                    */
  129.  
  130. #ifdef     NO_CTYPE_H                        /* WCC */
  131. int isalpha(), isdigit(), isupper(), toupper();
  132. #else
  133. #include <ctype.h>                        /* WHR */
  134. #endif
  135.  
  136. #ifdef     NO_STRING_H                        /* WCC */
  137. int strcmp();  char *strcpy();
  138. #else
  139. #include <string.h>
  140. #endif
  141.  
  142. #ifndef     abs                            /* WCC */
  143. #define     abs(x)        ((x < 0) ? (-x) : (x))
  144. #endif
  145.  
  146. /*  Portability Note:  Some stdio.h files define various of these
  147.     constants.    Some don't.  Therefore, each definition is
  148.     compiled only if needed.                       WCC */
  149.  
  150. #ifndef     NULL                            /* WCC */
  151. #define     NULL        0
  152. #endif
  153.  
  154. #ifndef     FALSE                            /* WCC */
  155. #define     FALSE        0                        /* WHR */
  156. #endif
  157.  
  158. #ifndef     TRUE                            /* WCC */
  159. #define     TRUE        1                        /* WHR */
  160. #endif
  161.  
  162. #define     MAX_REF    5        /* maximum refs per ref-block */
  163. #define     MAX_LEN    20        /* maximum identifier length  */
  164. #define     MAX_WRD   749        /* maximum number of identifiers */
  165. #define     MAX_ALPHA  53        /* maximum alpha chain heads */
  166. #define     REFS_PER_LINE    8    /* maximum refs per line */
  167. #define     LINES_PER_PAGE 60
  168. #define     FF 0x0C         /* formfeed */
  169.  
  170. /*  Order of the next two structure declarations reversed to avoid
  171.     a forward reference that chokes some compilers.            WCC */
  172.  
  173. struct rf_blk {
  174.          int ref_item[MAX_REF];
  175.          int ref_cnt;
  176.          struct rf_blk *ref_lnk;
  177.            } onerf;
  178.  
  179. struct id_blk {
  180.          char id_name[MAX_LEN];
  181.          struct id_blk *alp_lnk;
  182.          struct rf_blk *top_lnk;
  183.          struct rf_blk *lst_lnk;
  184.            } oneid, *id_vector[MAX_WRD];
  185.  
  186. struct alp_hdr { struct id_blk *alp_top;
  187.            struct id_blk *alp_lst;
  188.          } alp_vector[MAX_ALPHA];
  189.  
  190. int    linum;        /* line number */
  191. int    edtnum;        /* edit line number */
  192. int    fil_cnt;    /* active file index */
  193. int    wrd_cnt;    /* token count */
  194. int    pagno;        /* page number */
  195. int    id_cnt;        /* number of unique identifiers */
  196. int    rhsh_cnt;    /* number of conflict hits */
  197. int    filevl;        /* file level  */
  198. int    paglin;        /* page line counter */
  199. int    prt_ref = FALSE;
  200. char    act_fil[MAX_LEN];
  201. char    lst_fil[MAX_LEN];
  202. char    gbl_fil[MAX_LEN];
  203. FILE   *l_buffer;                         /* WHR */
  204. int    e_flg = FALSE, i_flg = FALSE, o_flg = FALSE;         /* WHR */
  205. int    r_flg = FALSE, l_flg = FALSE, debug = FALSE;         /* WCC */
  206. char    Epson[]     = "\033@\017\033Q\204";             /* WCC */
  207.  
  208. void exit();
  209.  
  210. /*-------------------------------------------*/
  211.  
  212. void main(argc,argv)
  213. int    argc;
  214. char    **argv;
  215. {
  216.      char  *arg, cc;
  217.      void nl(), prnt_tbl(), proc_file(), use_err();
  218.  
  219.      if (argc < 2) use_err();
  220.      (void)strcpy(gbl_fil,*++argv);
  221.      --argc;
  222.      if(gbl_fil[0] == '-')
  223.       use_err();
  224.      while(--argc != 0)
  225.      {      if(*(arg=*++argv) == '-')
  226.     /*****{    switch( toupper(*++arg) )     *** side effect in Microsoft C */
  227.       {    switch( cc=*++arg, toupper(cc) )             /* Microsoft C */
  228.            {   case 'I':  i_flg++;
  229.                   break;
  230.            case 'R':  r_flg++;
  231.                   break;
  232.            case 'L':  l_flg++;
  233.                   break;
  234.            case 'O': {o_flg++;
  235.                   if(--argc == 0) use_err();
  236.                   (void)strcpy(lst_fil,*++argv);
  237.                   if(lst_fil[0] == '-') use_err();
  238.                   if(debug) printf("lst_fil=>%s<",lst_fil);
  239.                   break;
  240.                  }
  241.            case 'D':  debug++;
  242.                   break;
  243.            case 'E':  e_flg++;                  /* WHR */
  244.                   o_flg++;
  245.                   (void)strcpy(lst_fil,"LPT1:");
  246.                   break;
  247.            default:   use_err();
  248.            }
  249.       }
  250.      else use_err();
  251.      }
  252.      if (debug) printf("\ni_flg=%d, r_flg=%d, l_flg=%d", i_flg,r_flg,l_flg);
  253.      if (debug) printf("\no_flg=%d, debug=%d", o_flg,debug);
  254.      if (o_flg)
  255.      {      if ( (l_buffer = fopen(lst_fil,"w")) == NULL)      /*** output file ***/
  256.       {    printf("ERROR Unable to create list file - %s\n",lst_fil);
  257.            exit(0);
  258.       }
  259.       printf("\nXC ....... 'C' Concordance Utility  v2.3\n");
  260.       printf("    Portable C,  Conversion 5 APR 1987\n\n");        /* WCC */
  261.       if (e_flg) fprintf(l_buffer,"%s",Epson);
  262.      }
  263.      for(linum=0;linum < MAX_WRD;linum++) {
  264.       id_vector[linum] = NULL;
  265.      }
  266.      for(linum=0;linum < MAX_ALPHA;linum++)
  267.      {
  268.       alp_vector[linum].alp_top =
  269.            alp_vector[linum].alp_lst = NULL;
  270.      }
  271.      fil_cnt = wrd_cnt = linum = 0;
  272.      filevl=paglin=pagno=edtnum=0;
  273.      id_cnt=rhsh_cnt=0;
  274.      proc_file(gbl_fil);
  275.      if(!l_flg) {
  276.      prnt_tbl();
  277.      printf("\nAllowable Symbols: %d\n",MAX_WRD);
  278.      printf("Unique    Symbols: %d\n",id_cnt);
  279.      }
  280.      if(o_flg) {
  281.       nl();
  282.       fprintf(l_buffer,"\nAllowable Symbols: %d\n",MAX_WRD);   /* WHR */
  283.       fprintf(l_buffer,"Unique    Symbols: %d\n",id_cnt);       /* WHR */
  284.       fprintf(l_buffer,"%c",FF);                   /* WHR */
  285.       fflush(l_buffer);
  286.       fclose(l_buffer);
  287.      }
  288. }/*main.
  289. ----------------------------------------*/
  290.  
  291. void use_err()
  292. {
  293.      printf("\nERROR: Invalid parameter specification\n\n");
  294.      printf("Usage: xc <filename> <flag(s)>\n\n");
  295.      printf("Flags: -e             = Emit printer initialization string\n");
  296.      printf("       -i             = Enable file inclusion\n");
  297.      printf("       -l             = Generate listing only\n");
  298.      printf("       -r             = Cross-reference reserved words\n");
  299.      printf("       -o <filename>  = Write output to named file\n");
  300.      exit(0);
  301. }/*use_err.
  302. -------------------------------------------*/
  303.  
  304.  
  305. void proc_file(filnam)
  306.      char    *filnam;
  307.  
  308. {
  309.      FILE  *buffer;       /* allocated buffer pointer */        /* WHR */
  310.      char  token[MAX_LEN]; /* token buffer */
  311.      int   eof_flg;       /* end-of-file indicator */
  312.      int   tok_len;       /* token length */
  313.      int   incnum;       /* included line number */
  314.      int chk_token(), get_token();
  315.      void nl(), prt_hdr(), put_token();
  316.  
  317.      (void)strcpy(act_fil,filnam);
  318.      if ((buffer = fopen(filnam,"r")) == NULL)    /*** input file ***/ /* WHR */
  319.      {      printf("\nERROR: Unable to open input file: %s\n",filnam);
  320.       exit(0);
  321.      }
  322.      if(filevl++ == 0) prt_hdr();
  323.      eof_flg = FALSE;
  324.  
  325.      do {
  326.       if(get_token(buffer,token,&tok_len,&eof_flg,0))
  327.       {    if (debug) printf("\ntoken: %s   length: %d\n",token,&tok_len);
  328.            if (chk_token(token))
  329.            {
  330.              /* #include processing changed to accept drive:   WHR */
  331.             if (strcmp(token,"#include") == 0)
  332.             {     if (get_token(buffer,token,&tok_len,&eof_flg,1))
  333.              {    if (debug) printf("\ntoken: %s   length: %d\n",
  334.                           token,&tok_len);
  335.                   if (!i_flg) continue;
  336.                   else
  337.                   {       incnum=edtnum;
  338.                    edtnum=0;
  339.                    nl();
  340.                    proc_file(token);
  341.                    edtnum=incnum;
  342.                    (void)strcpy(act_fil,filnam);
  343.                    continue;
  344.                   }
  345.              }
  346.             }
  347.             put_token(token,linum);
  348.            }
  349.       }
  350.      } while (!eof_flg);
  351.  
  352.      filevl -= 1;
  353.      fclose(buffer);
  354.  /***     free(sav_buffer);  */
  355. }/*proc_file.
  356. -------------------------------------------*/
  357.  
  358. int get_token(g_buffer,g_token,g_toklen,g_eoflg,g_flg)
  359.      FILE    *g_buffer;
  360.      char    *g_token;
  361.      int     *g_toklen;
  362.      int     *g_eoflg;
  363.      int     g_flg;
  364.  
  365.     /*
  366.      *     'getoken' returns the next valid identifier or
  367.      *     reserved word from a given file along with the
  368.      *     character length of the token and an end-of-file
  369.      *     indicator
  370.      *
  371.      */
  372.  
  373. {
  374.      int     c, rdchr();
  375.      char    *h_token;
  376.      char    tmpch;
  377.      char    tmpch2;              /* WHR fix for B:filename.ext */
  378.  
  379.      h_token = g_token;
  380.  
  381.  gtk:                   /* top of loop, get new token */
  382.      *g_toklen = 0;
  383.      g_token = h_token;
  384.  
  385.     /*
  386.      *    Scan and discard any characters until an alphabetic or
  387.      *    '_' (underscore) character is encountered or an end-of-file
  388.      *    condition occurs
  389.      */
  390.  
  391.      while(  (!isalpha(*g_token = rdchr(g_buffer,g_eoflg,g_flg)))
  392.          &&     !*g_eoflg    &&  *g_token != '_'
  393.          && *g_token != '0' &&  *g_token != '#' );
  394.      if(*g_eoflg) return(FALSE);
  395.  
  396.      *g_toklen += 1;
  397.  
  398.     /*
  399.      *     Scan and collect identified alpanumeric token until
  400.      *     a non-alphanumeric character is encountered or and
  401.      *     end-of-file condition occurs
  402.      */
  403.  
  404.      if(g_flg) {
  405.       tmpch     = '.';
  406.       tmpch2 = ':';              /* WHR fix for B:filename.ext */
  407.      }
  408.      else {
  409.       tmpch     = '_';
  410.       tmpch2 = '_';              /* WHR fix for B:filename.ext */
  411.      }
  412.      while( (isalpha(c=rdchr(g_buffer,g_eoflg,g_flg)) ||
  413.         isdigit(c) || c == '_' || c == tmpch || c == tmpch2)
  414.         && !*g_eoflg)           /* WHR fix for B:filename.ext */
  415.      {      if(*g_toklen < MAX_LEN)
  416.       {    *++g_token = c;
  417.            *g_toklen += 1;
  418.       }
  419.      }
  420.  
  421.  
  422.     /*
  423.      *        Check to see if a numeric hex or octal constant has
  424.      *        been encountered ... if so dump it and try again
  425.      */
  426.  
  427.  
  428.      if (*h_token == '0') goto gtk;
  429.  
  430.  
  431.     /*
  432.      *        Tack a NULL character onto the end of the token
  433.      */
  434.  
  435.      *++g_token = NULL;
  436.  
  437.     /*
  438.      *        Screen out all #token strings except #include
  439.      */
  440.  
  441.      if (*h_token == '#' && strcmp(h_token,"#include")) goto gtk;
  442.  
  443.      return (TRUE);
  444. }/*get_token.
  445. -----------------------------------------*/
  446.  
  447. int fil_chr(f_buffer,f_eof)
  448.      FILE *f_buffer;
  449.      int *f_eof;
  450. {
  451.      int fc;
  452.      if ((fc = getc(f_buffer)) == EOF) {
  453.       *f_eof = TRUE;
  454.       fc = NULL;
  455.      }
  456.      return(fc);
  457. }/*fil_chr.
  458. ----------------------------------------*/
  459.  
  460. int rdchr(r_buffer,r_eoflg,rd_flg)
  461.      int     *r_eoflg, rd_flg;
  462.      FILE    *r_buffer;
  463.  
  464. /*
  465.     'rdchr' returns the next valid character in a file
  466.     and an end-of-file indicator. A valid character is
  467.     defined as any which does not appear in either a
  468.     commented or a quoted string ... 'rdchr' will correctly
  469.     handle comment tokens which appear within a quoted
  470.     string
  471. */
  472.  
  473. {
  474.      int     c;
  475.      int     q_flg;         /* double quoted string flag */
  476.      int     q1_flg;         /* single quoted string flag */
  477.      int     cs_flg;         /* comment start flag */
  478.      int     ce_flg;         /* comment end flag */
  479.      int     c_cnt;         /* comment nesting level */
  480.      int     t_flg;         /* transparency flag */
  481.      void nl();
  482.  
  483.      q_flg = q1_flg = cs_flg = ce_flg = t_flg = FALSE;
  484.      c_cnt  = 0;
  485.  
  486. rch:
  487.  
  488.     /*
  489.      *     Fetch character from file
  490.      */
  491.  
  492.      c = fil_chr(r_buffer,r_eoflg);
  493.  
  494.      if (*r_eoflg) return(c);    /* EOF encountered */
  495.      if (c == '\n')
  496.        nl();
  497.      else
  498.       if (o_flg) fprintf(l_buffer,"%c",c);            /* WHR */
  499.       else printf("%c",c);
  500.  
  501.      if (rd_flg) return(c);
  502.  
  503.      if (t_flg) {
  504.       t_flg = !t_flg;
  505.       goto rch;
  506.      }
  507.  
  508.      if (c == '\\') {
  509.       t_flg = TRUE;
  510.       goto rch;
  511.      }
  512.     /*
  513.     If the character is not part of a quoted string
  514.     check for and process commented strings...
  515.     nested comments are handled correctly but unbalanced
  516.     comments are not ... the assumption is made that
  517.     the syntax of the program being xref'd is correct
  518.     */
  519.  
  520.      if (!q_flg     &&  !q1_flg) {
  521.       if (c == '*'    &&  c_cnt  &&  !cs_flg) {
  522.            ce_flg = TRUE;
  523.            goto rch;
  524.       }
  525.       if (c == '/'    &&  ce_flg) {
  526.            c_cnt -= 1;
  527.            ce_flg = FALSE;
  528.            goto rch;
  529.       }
  530.       ce_flg = FALSE;
  531.       if (c == '/') {
  532.            cs_flg = TRUE;
  533.            goto rch;
  534.       }
  535.       if (c == '*'    &&  cs_flg) {
  536.            c_cnt += 1;
  537.            cs_flg = FALSE;
  538.            goto rch;
  539.       }
  540.       cs_flg = FALSE;
  541.  
  542.       if (c_cnt) goto rch;
  543.      }
  544.  
  545.     /*
  546.     Check for and process quoted strings
  547.     */
  548.  
  549.      if ( c == '"'  &&    !q1_flg) {    /* toggle quote flag */
  550.       q_flg =  !q_flg;
  551.       if(debug) printf("\nq_flg toggled to: %d\n" ,q_flg);
  552.       goto rch;
  553.      }
  554.      if (q_flg) goto rch;
  555.  
  556.      if (c == '\'') {         /* toggle quote flag */
  557.       q1_flg = !q1_flg;
  558.       if(debug) printf("\nq1_flg toggled to: %d\n" ,q1_flg);
  559.       goto rch;
  560.      }
  561.      if (q1_flg) goto rch;
  562.  
  563.     /*
  564.     Valid character ... return to caller
  565.     */
  566.  
  567.      return (c);
  568. }/*rdchr.
  569. -----------------------------------------------*/
  570.  
  571.  
  572. int chk_token(c_token)
  573.      char    *c_token;
  574. {
  575.      char  u_token[MAX_LEN];
  576.      int   i;
  577.  
  578.      {
  579.       if (r_flg) return(TRUE);
  580.       i = 0;
  581.       do { u_token[i] = toupper(c_token[i]);
  582.       } while (c_token[i++] != NULL);
  583.  
  584.       switch(u_token[0]) {
  585.     case 'A': if (strcmp(u_token,"AUTO") == 0) return(FALSE);
  586.           break;
  587.     case 'B': if (strcmp(u_token,"BREAK") == 0) return(FALSE);
  588.           break;
  589.     case 'C': if (strcmp(u_token,"CHAR") == 0) return (FALSE);
  590.           if (strcmp(u_token,"CONTINUE") == 0) return (FALSE);
  591.           if (strcmp(u_token,"CASE") == 0) return (FALSE);
  592.           break;
  593.  
  594.     case 'D': if(strcmp(u_token,"DOUBLE") == 0) return(FALSE);
  595.           if(strcmp(u_token,"DO") == 0) return(FALSE);
  596.           if(strcmp(u_token,"DEFAULT") == 0) return(FALSE);
  597.           break;
  598.     case 'E': if(strcmp(u_token,"EXTERN") == 0) return(FALSE);
  599.           if(strcmp(u_token,"ELSE") == 0) return(FALSE);
  600.           if(strcmp(u_token,"ENTRY") == 0) return(FALSE);
  601.           break;
  602.     case 'F': if(strcmp(u_token,"FLOAT") == 0) return(FALSE);
  603.           if(strcmp(u_token,"FOR") == 0) return(FALSE);
  604.           break;
  605.     case 'G': if(strcmp(u_token,"GOTO") == 0) return(FALSE);
  606.           break;
  607.     case 'I': if(strcmp(u_token,"INT") == 0) return(FALSE);
  608.           if(strcmp(u_token,"IF") == 0) return(FALSE);
  609.           break;
  610.     case 'L': if(strcmp(u_token,"LONG") == 0) return(FALSE);
  611.           break;
  612.     case 'R': if(strcmp(u_token,"RETURN") == 0) return(FALSE);
  613.           if(strcmp(u_token,"REGISTER") == 0) return(FALSE);
  614.           break;
  615.     case 'S': if(strcmp(u_token,"STRUCT") == 0) return(FALSE);
  616.           if(strcmp(u_token,"SHORT") == 0) return(FALSE);
  617.           if(strcmp(u_token,"STATIC") == 0) return(FALSE);
  618.           if(strcmp(u_token,"SIZEOF") == 0) return(FALSE);
  619.           if(strcmp(u_token,"SWITCH") == 0) return(FALSE);
  620.           break;
  621.     case 'T': if(strcmp(u_token,"TYPEDEF") == 0) return(FALSE);
  622.           break;
  623.     case 'U': if(strcmp(u_token,"UNION") == 0) return(FALSE);
  624.           if(strcmp(u_token,"UNSIGNED") == 0) return(FALSE);
  625.           break;
  626.     case 'W': if(strcmp(u_token,"WHILE") == 0) return(FALSE);
  627.           break; }
  628.     }
  629.   return (TRUE);
  630. }/*chk_token.
  631. ---------------------------------------------*/
  632.  
  633.  
  634.   /*
  635.    *    Install parsed token and line reference in linked structure
  636.    */
  637.  
  638. void put_token(p_token,p_ref)
  639. char *p_token;
  640. int  p_ref;
  641. {
  642.      int  d, found, hsh_index, i, j;
  643.      struct id_blk *idptr, *allo_id();
  644.      struct rf_blk *allo_rf(), *add_rf();
  645.      void chain_alpha();
  646.  
  647.      if (l_flg) return;
  648.      j=0;
  649.      for (i=0; p_token[i] != NULL; i++)     /* Hashing algorithm is far from */
  650.      {                     /* optimal but is adequate for a */
  651.      j = j * 10 + p_token[i];     /* memory-bound index vector!      */
  652.      }
  653.      hsh_index = abs(j) % MAX_WRD;
  654.  
  655.      found = FALSE;
  656.      d = 1;
  657.      do
  658.      {      idptr = id_vector[hsh_index];
  659.       if (idptr == NULL)
  660.       {    id_cnt++;
  661.            idptr = id_vector[hsh_index] = allo_id(p_token);
  662.            chain_alpha(idptr,p_token);
  663.            idptr->top_lnk = idptr->lst_lnk = allo_rf(p_ref);
  664.            found = TRUE;
  665.       }
  666.       else
  667.       if (strcmp(p_token,idptr->id_name) == 0)
  668.       {    idptr->lst_lnk = add_rf(idptr->lst_lnk,p_ref);
  669.            found = TRUE;
  670.       }
  671.       else
  672.       {    hsh_index += d;
  673.            d += 2;
  674.            rhsh_cnt++;
  675.            if (hsh_index >= MAX_WRD)
  676.             hsh_index -= MAX_WRD;
  677.            if (d == MAX_WRD)
  678.            {    printf("\nERROR: Symbol table overflow\n");
  679.             exit(0);
  680.            }
  681.       }
  682.      } while (!found);
  683. }/*put_token.
  684. --------------------------------------------*/
  685.  
  686.  
  687. void chain_alpha(ca_ptr,ca_token)
  688. struct id_blk *ca_ptr;
  689. char  *ca_token;
  690. {
  691.      char  c;
  692.      struct id_blk *cur_ptr;
  693.      struct id_blk *lst_ptr;
  694.  
  695.      c = ca_token[0];
  696.      if (c == '_')  c = 0;
  697.      else
  698.      /**  isupper(c) ? c=1+((c-'A')*2) : c=2+((c-'a')*2) ;     error or not??
  699.       **  A good one for the puzzle book! Is the () required around (c=..)?
  700.       **  C86 and Microsoft C both req the ()'s, BDS C did not.
  701.       **  Is it required because = has lower precedence than ?: ????
  702.       **/
  703.       isupper(c) ? (c=1+((c-'A')*2)) : (c=2+((c-'a')*2)) ;
  704.  
  705.      if(alp_vector[c].alp_top == NULL)
  706.      {      alp_vector[c].alp_top =
  707.           alp_vector[c].alp_lst = ca_ptr;
  708.       ca_ptr->alp_lnk = NULL;
  709.       return;
  710.      }
  711.  
  712.     /*    check to see if new id_blk should be inserted between
  713.      *    the alp_vector header block and the first id_blk in
  714.      *    the current alpha chain
  715.      */
  716.  
  717.      if(strcmp(alp_vector[c].alp_top->id_name,ca_token) >0)
  718.      {      ca_ptr->alp_lnk=alp_vector[c].alp_top;
  719.       alp_vector[c].alp_top=ca_ptr;
  720.       return;
  721.      }
  722.  
  723.      if(strcmp(alp_vector[c].alp_lst->id_name,ca_token) < 0)
  724.      {      alp_vector[c].alp_lst->alp_lnk = ca_ptr;
  725.       ca_ptr->alp_lnk = NULL;
  726.       alp_vector[c].alp_lst=ca_ptr;
  727.       return;
  728.      }
  729.  
  730.      cur_ptr = alp_vector[c].alp_top;
  731.      while(strcmp(cur_ptr->id_name,ca_token) < 0)
  732.      {      lst_ptr = cur_ptr;
  733.       cur_ptr = lst_ptr->alp_lnk;
  734.      }
  735.  
  736.      lst_ptr->alp_lnk = ca_ptr;
  737.      ca_ptr->alp_lnk = cur_ptr;
  738.      return;
  739. }/*chain_alpha.
  740. -----------------------------------------*/
  741.  
  742. struct id_blk *allo_id(aid_token)
  743. char  *aid_token;
  744. {
  745.      int  ai;
  746.      struct id_blk *aid_ptr;
  747.      char *malloc();                         /* Microsoft C */
  748.  
  749. /*** if((aid_ptr =  alloc(sizeof(oneid))) == 0) { ***/         /* Microsoft C */
  750.      if((aid_ptr = (struct id_blk *) malloc(sizeof(oneid))) == 0) { /* MS C */
  751.  
  752.       printf("\nERROR: Unable to allocate identifier block\n");
  753.       exit(0);
  754.      }
  755.      ai=0;
  756.      do {
  757.       aid_ptr->id_name[ai] = aid_token[ai];
  758.      } while (aid_token[ai++] != NULL);
  759.      return (aid_ptr);
  760. }/*id_blk.
  761. -----------------------------------------*/
  762.  
  763. struct rf_blk *allo_rf(arf_ref)
  764. int  arf_ref;
  765. {
  766.     int ri;
  767.     struct rf_blk *arf_ptr;
  768.     char *malloc();                       /* Microsoft C */
  769.  
  770. /** if((arf_ptr = alloc(sizeof(onerf))) == 0) { **/    /* Microsoft C */
  771.     if((arf_ptr = (struct rf_blk *) malloc(sizeof(onerf))) == 0) { /* MS C */
  772.      printf("\nERROR: Unable to allocate reference block\n");
  773.      exit(0);
  774.     }
  775.     arf_ptr->ref_item[0] = arf_ref;
  776.     arf_ptr->ref_cnt = 1;  arf_ptr->ref_lnk = NULL;
  777.     for (ri=1;ri<MAX_REF;ri++)
  778.       arf_ptr->ref_item[ri] = NULL;
  779.     return (arf_ptr);
  780. }/*allo_rf.
  781. ------------------------------------------*/
  782.  
  783.  
  784. struct rf_blk *add_rf(adr_ptr,adr_ref)
  785. struct rf_blk *adr_ptr;
  786. int adr_ref;
  787. {
  788.      if(adr_ptr->ref_cnt == MAX_REF)
  789.       return adr_ptr->ref_lnk = allo_rf(adr_ref);
  790.      else {
  791.       adr_ptr->ref_item[adr_ptr->ref_cnt++] = adr_ref;
  792.       return (adr_ptr);
  793.      }
  794. }/*rf_blk.
  795. ------------------------------------------*/
  796.  
  797. void prnt_tbl()
  798. {
  799.      int lin_cnt, pref, prf_cnt, pti;
  800.      struct id_blk *pid_ptr;
  801.      struct rf_blk *ptb_ptr;
  802.      void nl(), prt_hdr();
  803.  
  804.      prt_ref = TRUE;
  805.      prt_hdr();
  806.      for (pti=0;pti<MAX_ALPHA;pti++)
  807.      {      if ((pid_ptr = alp_vector[pti].alp_top) != NULL)
  808.       {    do
  809.            {    if(o_flg)                          /* WHR */
  810.              fprintf(l_buffer,"%-14.13s: ",pid_ptr->id_name);
  811.             else printf("%-14.13s: ",pid_ptr->id_name);
  812.             ptb_ptr=pid_ptr->top_lnk;
  813.             lin_cnt=prf_cnt=0;
  814.             do
  815.             {     if(prf_cnt == MAX_REF)
  816.              {    prf_cnt=0;
  817.                   ptb_ptr = ptb_ptr->ref_lnk;
  818.              }
  819.              if(ptb_ptr)
  820.              {
  821.                   if((pref=ptb_ptr->ref_item[prf_cnt++]) != 0)
  822.                   {
  823.                    if (lin_cnt++ == REFS_PER_LINE)
  824.                    {    nl();             /* WHR,WCC */
  825.                     if (o_flg) fprintf(l_buffer,"\t\t");
  826.                     else printf("\t\t");
  827.                     lin_cnt = 1;
  828.                    }
  829.                    if(o_flg) fprintf(l_buffer,"%-4d  ",pref);
  830.                    else printf("%-4d  ",pref);
  831.                   }
  832.              }
  833.              else pref=0;
  834.             } while (pref);
  835.             nl();
  836.            } while ((pid_ptr=pid_ptr->alp_lnk) != NULL);
  837.       }
  838.      }
  839. }/*prnt_tbl.
  840. ---------------------------------------*/
  841.  
  842.  
  843. void prt_hdr()
  844. {
  845.      void nl();
  846.  
  847.      if (pagno++ != 0)
  848.      if(o_flg) fprintf(l_buffer,"%c",FF);            /* WHR */
  849.      else printf("%c",FF);
  850.  
  851.      if(o_flg)
  852.       fprintf(l_buffer,
  853.            "XC ... 'C' Concordance Utility : %s\t\t\t\t Page %d",
  854.            gbl_fil,pagno);                       /* WHR */
  855.      else printf("XC ... 'C' Concordance Utility : %s\t\t\t\t Page %d",
  856.       gbl_fil,pagno);
  857.      if (o_flg) fprintf(l_buffer,"\n\n");               /* WHR */
  858.      else printf("\n\n");
  859.  
  860.      nl();  paglin =3;    return;
  861. }/*prt_hdr.
  862. --------------------------------------*/
  863.  
  864. void nl()
  865. {
  866.      if (o_flg) fprintf(l_buffer,"\n");                   /* WHR */
  867.      else printf("\n");
  868.  
  869.      if (++paglin == LINES_PER_PAGE) prt_hdr();
  870.      else if(!prt_ref)
  871.       {    if (o_flg) fprintf(l_buffer,"%-4d %4d:\t",
  872.                 ++linum,++edtnum);           /* WHR */
  873.            else printf("%-4d %4d:\t",++linum,++edtnum);
  874.  
  875.            if(o_flg)
  876.             if(linum % 60 == 1) printf("\n<%d>\t",linum);
  877.             else printf(".");
  878.       }
  879.      return;
  880. }/*nl.
  881. -------------------------------------------*/
  882. /*============= end of file xc.c ==========================*/
  883.  
  884.